package io.github.wjehovah.yyn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

import io.github.wjehovah.yyn.adapter.CommentAdaper;
import io.github.wjehovah.yyn.bean.CommBean;
import io.github.wjehovah.yyn.bean.CommentBean;

public class CommentActivity extends Activity {

    private TextView commentNumber;
    private TextView forward;
    private TextView hate;
    private TextView love;
    private TextView content;
    private TextView passtime;
    private TextView username;
    private ImageView usericon;
    private String type1;
    private CommBean.BeanDetail beanDetail;
    private ImageView iv_content;
    private TextView urlVideo;
    private CommentBean commentBean;
    private int size;
    private ListView listView;
    private CommentAdaper commentAdaper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        beanDetail = (CommBean.BeanDetail) bundle.getParcelable("beanDetail");
        String url = "http://api.budejie.com/api/api_open.php?&a=dataList&c=comment&page=1&per=20&data_id=" + beanDetail.id + "&hot=1";
        HttpUtils httpUtils = new HttpUtils(1000);
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String json = responseInfo.result;
                Gson gson = new Gson();
                commentBean = gson.fromJson(json, CommentBean.class);
                size = commentBean.hot.size();//热门条数；

                List<CommentBean.DataInfo> data_latest = commentBean.data;//最新的评论
                List<CommentBean.DataInfo> data_hot = commentBean.hot;//热门的;

                listView = (ListView) findViewById(R.id.lv_comment);
                commentAdaper = new CommentAdaper(getApplicationContext(), data_hot, data_latest);

                listView.setAdapter(commentAdaper);
//                int count =data_hot.size()+data_latest.size()+2;
                setListViewHeightBasedOnChildren(listView);
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
        if (beanDetail == null) {
            return;
        }
        type1 = beanDetail.type;
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fl_all_content);
        if (type1.equals("29")) {
            frameLayout.setVisibility(View.GONE);
            initViewDuanzi();
        } else if (type1.equals("10")) {//图片
            frameLayout.setVisibility(View.VISIBLE);
            initViewImage();
        } else if (type1.equals("41")) {//视频
            frameLayout.setVisibility(View.VISIBLE);
            initViewVideo();
        }
    }

    private void initViewImage() {//图片；
        initViewDuanzi();
        iv_content = (ImageView) findViewById(R.id.iv_content);
        BitmapUtils bitmaputils = new BitmapUtils(getApplication());
        bitmaputils.display(iv_content, beanDetail.image1);
    }

    private void initViewVideo() {//视频
        initViewDuanzi();
        urlVideo = (TextView) findViewById(R.id.tv_comment111_picture);
        urlVideo.setText(beanDetail.videouri);
    }

    private void initViewDuanzi() {
        usericon = (ImageView) findViewById(R.id.iv_top_item_usericon);//用户头像
        username = (TextView) findViewById(R.id.tv_top_item_username);//用户名
        passtime = (TextView) findViewById(R.id.tv_top_item_passtime);//审核通过时间
        content = (TextView) findViewById(R.id.tv_essenece_duanzi_content);//内容

        love = (TextView) findViewById(R.id.tv_foot_love);//顶
        hate = (TextView) findViewById(R.id.tv_foot_hate);//踩
        forward = (TextView) findViewById(R.id.tv_foot_transmit);//分享
        commentNumber = (TextView) findViewById(R.id.tv_foot_commnet);//评论

        username.setText(beanDetail.screen_name);//用户名
        passtime.setText(beanDetail.created_at);//时间
        content.setText(beanDetail.text);//内容
        love.setText(beanDetail.love);//顶
        hate.setText(beanDetail.hate);//踩
        forward.setText(beanDetail.repost);//分享
        commentNumber.setText(beanDetail.comment);//评论
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;

        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        Log.e("LCy", "listAdapter.getCount()==" + listAdapter.getCount());
        Log.e("LCy", "totalHeight==" + totalHeight);
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
